package com.mk.admin.service.impl;

import com.mk.admin.mapper.AdminMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.AdminDoctorDto;
import com.mk.admin.model.dto.AdminRoleSysmenuDto;
import com.mk.admin.model.dto.LoginDto;
import com.mk.admin.model.dto.RAdminDoctorDto;
import com.mk.admin.model.entity.AdminEntity;
import com.mk.admin.model.entity.LoginEntity;
import com.mk.admin.model.entity.TableEntity;
import com.mk.admin.model.query.AdminQuery;
import com.mk.admin.model.query.TableQuery;
import com.mk.admin.model.vo.AdminVo;
import com.mk.admin.model.vo.LoginVo;
import com.mk.admin.service.AdminService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ResultDto select(AdminQuery query) {
        return ResultDto.builder()
                .result(adminMapper.select(JSONCopy.copy(query, AdminEntity.class)))
                .count(adminMapper.count(JSONCopy.copy(query, AdminEntity.class)))
                .retCode("T")
                .build();
    }

    @Override
    public ResultDto selectDoctorInfo(AdminVo vo) {
        AdminEntity entity = new AdminEntity();

        List<AdminDoctorDto> list = adminMapper.selectDoctorInfo(entity);
        return ResultDto.data(list);
    }

    @Override
    public ResultDto selectRoleSysMenuByUsernameOrPhone(AdminVo vo) {
        AdminEntity entity = JSONCopy.copy(vo, AdminEntity.class);
        List<AdminRoleSysmenuDto> adminRoleSysmenuDtos = adminMapper.selectRoleSysMenuByUsernameOrPhone(entity);
        return ResultDto.data(adminRoleSysmenuDtos);
    }

    @Override
    public ResultDto allTable(TableQuery query) {
        TableEntity tableEntity = JSONCopy.copy(query, TableEntity.class);
        return ResultDto.data(adminMapper.allTableCount(tableEntity), adminMapper.allTable(tableEntity));
    }

    @Override
    public ResultDto add(AdminVo vo) {
//        新增之前需要判断当前用户账号是否存在，
        vo.setCreateTime(DateTool.format());
        return ResultDto.data(adminMapper.insert(JSONCopy.copy(vo, AdminEntity.class)));
    }

    @Override
    public ResultDto update(AdminVo vo) {
        vo.setUpdateTime(DateTool.format());
        return ResultDto.data(adminMapper.updateById(JSONCopy.copy(vo, AdminEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(adminMapper.deleteById(id));
    }


    @Override
    public ResultDto status(int id) {
        return ResultDto.data(adminMapper.status(id));
    }

    @Override
    public ResultDto login(LoginVo vo) {
        LoginEntity entity = JSONCopy.copy(vo, LoginEntity.class);
        LoginDto login = adminMapper.login(entity);
        if (login == null) {
            return ResultDto.data("error", "用户名或者密码不正确");
        }
//查询当前用户对应的角色和菜单
        AdminEntity adminEntity = new AdminEntity();
        adminEntity.setUsername(vo.getUsername());
        List<AdminRoleSysmenuDto> adminRoleSysmenuDtos = adminMapper.selectRoleSysMenuByUsernameOrPhone(adminEntity);
        return ResultDto.data(adminRoleSysmenuDtos);
    }

    @Override
    public ResultDto deleteBatchIds(List<AdminVo> list) {
        List<Integer> ids = list.stream().map(AdminVo::getId).collect(Collectors.toList());
        return ResultDto.data(adminMapper.deleteBatchIds(ids));
    }
}
